#include <fstream>
#include <string>
#include <list>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include "Client.hpp"

void Client::save(const std::string &filename)
{
  std::ofstream ofs(filename.c_str());
  boost::archive::xml_oarchive xml(ofs);
  xml << boost::serialization::make_nvp("User", *this);
}

Client *Client::load(const std::string& filename)
{
  Client *clt = new Client();
  std::ifstream ifs(filename.c_str());
  boost::archive::xml_iarchive xml(ifs);
  xml >> boost::serialization::make_nvp("User", *clt);
  return clt;
}

void Client::removeFriends(const std::string &friends)
{
  std::list<ClientCategory*>::iterator it;
  std::list<ClientCategory*> rm;
  for (it = this->_friends.begin(); it != this->_friends.end(); ++it)
    {
      if ((*it)->getLogin() == friends)
	rm.push_back(*it);
    }
  for (it = rm.begin(); it != rm.end(); ++it)
    {
      this->_friends.remove(*it);
      delete *it;
    }
  this->save();
}

bool Client::friendsExist(const std::string &login)
{
 std::list<ClientCategory*>::iterator it;
  for (it = this->_friends.begin(); it != this->_friends.end(); ++it)
    {
      if ((*it)->getLogin() == login)
	return true;
    }
  return false;
}
